package com.yuma.delegate.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class EnhancerTest {
    public static void main(String[] args) {
        // 被代理的对象
        Singer singer = new Singer();

        Enhancer enhancer = new Enhancer();
        // 父类
        enhancer.setSuperclass(Singer.class);
        // 设置接口
        enhancer.setInterfaces(Singer.class.getInterfaces());
        enhancer.setCallback(new MethodInterceptor(){

            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                System.out.println("某明星唱歌前");
                method.invoke(singer, args);
                System.out.println("某明星唱歌后");
                return null;
            }
        });
        Singer delegate = (Singer) enhancer.create();

        delegate.sing();
    }
}
